package cn.cyh.org.controller;

import cn.cyh.basic.uitls.AJaxResult;
import cn.cyh.basic.uitls.FastDfsUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/fastDfs")
public class FastDfsController {
    // 上传接口
    @PostMapping
    public AJaxResult upLoad(@RequestPart(required = true,value = "file")MultipartFile file){
        try {
            // 获取原始文件名字
            String filename = file.getOriginalFilename();
            // 截取文件后缀
            String substring = filename.substring(filename.lastIndexOf(",") + 1);
            // 获取路径地址
            String path = FastDfsUtil.upload(file.getBytes(), substring);
            // 返回到前端
            return AJaxResult.myself().setResultObj(path);
        } catch (Exception e) {
            e.printStackTrace();
            return AJaxResult.myself().setMsg("系统繁忙");
        }
    }
    // 删除接口
    @DeleteMapping
    public AJaxResult delete(@RequestParam("path") String path){
        try {
            // 获取地址名  去掉第一个斜杠 goup1/xxxxx/yyyy
            String pathTmp = path.substring(1);
            // 获取组名 goup1
            String groupName =  pathTmp.substring(0, pathTmp.indexOf("/")); //goup1
            // 获取第一个斜杠后的
            String remotePath = pathTmp.substring(pathTmp.indexOf("/")+1);// /xxxxx/yyyy
            FastDfsUtil.delete(groupName, remotePath);
            return  AJaxResult.myself().setResultObj(path);

        } catch (Exception e) {
            e.printStackTrace();
            return AJaxResult.myself().setMsg("系统繁忙");
        }
    }
}
